package com.aimaterials.resource.service.impl;

import java.util.*;

import com.aimaterials.common.utils.DateUtils;
import com.aimaterials.common.utils.uuid.IdUtils;
import com.aimaterials.resource.domain.ResResource;
import com.aimaterials.resource.mapper.ResResourceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.aimaterials.resource.mapper.ResStereoscopicMapper;
import com.aimaterials.resource.domain.ResStereoscopic;
import com.aimaterials.resource.service.IResStereoscopicService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 立体教材Service业务层处理
 * 
 * @author DaemonSu
 * @date 2020-11-10
 */
@Service
public class ResStereoscopicServiceImpl implements IResStereoscopicService 
{
    @Autowired
    private ResStereoscopicMapper resStereoscopicMapper;

    @Autowired
    private ResResourceMapper resResourceMapper;

    /**
     * 查询立体教材
     * 
     * @param stereoscopicId 立体教材ID
     * @return 立体教材
     */
    @Override
    public ResStereoscopic selectResStereoscopicById(Long stereoscopicId)
    {
        return resStereoscopicMapper.selectResStereoscopicById(stereoscopicId);
    }

    @Override
    public ResResource selectResByQrCode(String qrcode,String uuid) {
        return resResourceMapper.selectResResourceByQRCode(qrcode,uuid);
    }

    /**
     * 查询立体教材列表
     * 
     * @param resStereoscopic 立体教材
     * @return 立体教材
     */
    @Override
    public List<ResStereoscopic> selectResStereoscopicList(ResStereoscopic resStereoscopic)
    {
        return resStereoscopicMapper.selectResStereoscopicList(resStereoscopic);
    }

    /**
     * 新增立体教材
     * 
     * @param resStereoscopic 立体教材
     * @return 结果
     */
    @Override
    public int insertResStereoscopic(ResStereoscopic resStereoscopic)
    {
        resStereoscopic.setCreateTime(DateUtils.getNowDate());
        return resStereoscopicMapper.insertResStereoscopic(resStereoscopic);
    }

    /**
     * 修改立体教材
     * 
     * @param resStereoscopic 立体教材
     * @return 结果
     */
    @Override
    public int updateResStereoscopic(ResStereoscopic resStereoscopic)
    {
        resStereoscopic.setUpdateTime(DateUtils.getNowDate());
        return resStereoscopicMapper.updateResStereoscopic(resStereoscopic);
    }

    /**
     * 批量删除立体教材
     * 
     * @param stereoscopicIds 需要删除的立体教材ID
     * @return 结果
     */
    @Override
    public int deleteResStereoscopicByIds(Long[] stereoscopicIds)
    {
        return resStereoscopicMapper.deleteResStereoscopicByIds(stereoscopicIds);
    }

    /**
     * 批量删除立体教材中的资源
     *
     * @param resourceIds 需要删除的立体教材中资源的ID
     * @param sterId 教材ID
     * @return 结果
     */
    @Override
    public int deleteResByIds(Long[] resourceIds,Long sterId){
       return resStereoscopicMapper.deleteResByIds(resourceIds,sterId);
    }

    /**
     *更新教材资源
     *
     * 基本算法：找出教材原有的资源id，对比现在的资源id，从而找出真正新增和真正删除的资源，只对这些资源才做，
     * 没有变化的资源维持不动
     * @param resourceIds 资源的ID
     * @param  sterId   立体教材ID
     * @return
     */
    @Override
    public int updateResByIds(Long[] resourceIds, Long sterId) {
        ResResource resResource =new ResResource();
        resResource.setStereoscopicId(sterId);
        //获得现有教材的资源列表
        List<ResResource> oldlist=resResourceMapper.selectResResourceList(resResource);
        Long[] oldIds=new Long[oldlist.size()];
        for(int i=0;i<oldIds.length;i++){
            oldIds[i]=oldlist.get(i).getResourceId();
        }

        List<Long> oldL = Arrays.asList(oldIds);
        List<Long> newL = Arrays.asList(resourceIds);

        // 创建集合
        Collection<Long> realOld = new ArrayList<Long>(oldL);
        Collection<Long> realNew = new ArrayList<Long>(newL);

        Collection<Long> tempNew = new ArrayList<Long>(newL);

        //查找出新增加的资源
        realNew.removeAll(realOld);
        //查找出删除的资源
        realOld.removeAll(tempNew);
        int num=0;
        //删除真正删除的资源
        if(realOld.size()>0){
            Long[] temp=new Long[realOld.size()];
            num+= resStereoscopicMapper.deleteResByIds(realOld.toArray(temp),sterId);
        }
       if(realNew.size()>0){
           Long[] temp=new Long[realNew.size()];
           String[] arCodes=new String[realNew.size()];
           for(int i=0;i<realNew.size();i++){
               arCodes[i]=IdUtils.simpleUUID();
           }
           num+=   resStereoscopicMapper.addResByIds(realNew.toArray(temp),arCodes,sterId);
       }
        //插入真正新增的资源
        return  num;
    }

    /**
     * 修改资源二维码
     * @param resourceId 资源的ID
     * @param  sterId   立体教材ID
     * @param qrcode
     * @return
     */
    public int updateQRCode(Long resourceId,Long sterId,String qrcode){
        int  result=0;
        try {
            result =resStereoscopicMapper.updateQRCode(resourceId,sterId,qrcode);
        }catch (Exception e){
            e.printStackTrace();
            result=0;
        }
        return result;
    }

    /**
     * 删除立体教材信息
     * 
     * @param stereoscopicId 立体教材ID
     * @return 结果
     */
    @Override
    public int deleteResStereoscopicById(Long stereoscopicId)
    {
        return resStereoscopicMapper.deleteResStereoscopicById(stereoscopicId);
    }

    /**
     * 删除学校选用的 立体教材
     *
     * @param stereoscopicIds 教材ID
     * @param deptId          学校ID
     * @return
     */
    @Override
    public int deleteResStereoscopicInDept(Long[] stereoscopicIds, Long deptId) {

        return resStereoscopicMapper.deleteBookInDeptByID(stereoscopicIds,deptId);
    }

    /**
     * 保存学校选用的智慧教材
     *
     * @param stereoscopicIds 教材ID
     * @param deptId          学校ID
     * @return
     */
    @Override
    @Transactional
    public int saveResStereoscopicInDept(Long[] stereoscopicIds, Long deptId) {
        //先删除该单位所哟的教材
        resStereoscopicMapper.deleteBookInDept(deptId);
        //然后保存
        return resStereoscopicMapper.addBookInDept(stereoscopicIds,deptId);


    }
}
